;
(function (window, angular) {
	'use strict';
	angular.module("Controller.Corruption.InvoiceNotSignedDbd", [])
		.controller("InvoiceNotSignedDbdCtrl", ["$scope", "ybWidget", "ybHttp", "MESG_TYPE", "$filter", "$timeout", "UserContextService","$stateParams", function ($scope, ybWidget, ybHttp, MESG_TYPE, $filter, $timeout, UserContextService,$stateParams) {
			$scope.markInfo = {
				allChecked: false,
				curRow: -1
			};
			$scope.pageInfo = {
				totalPages: 1,
				totalRows: 0,
				pageSize: 10,
				curPage: 1
			};
			$scope.toolbarConfig = {
				showColConfig: false
			};


			$scope.condition = {};
			$scope.dataList = [];
			$scope.tableConfig = {
				fixedWidth: 100,
				scrollWidth: 1400,
				tableCol: [
					{
						name: "代理商",
						dataField: "dealerName",
						width: 160,
						isShow: true,
						isFixed: false,
					},
					{
						name: "开票凭证",
						dataField: "invoiceNo",
						width: 160,
						isShow: true,
						isFixed: false,
					},
					{
						name: "过帐时间",
						dataField: "postDateShow",
						width: 140,
						isShow: true,
						isFixed: false,
					},
					{
						name: "状态",
						dataField: "isSignShow",
						width: 120,
						isShow: true,
						isFixed: false,
					},
					{
						name: "预警时间",
						dataField: "warnDateShow",
						width: 140,
						isShow: true,
						isFixed: false,
					},
					{
						name: "预警时长（按天）",
						dataField: "warnDay",
						width: 140,
						isShow: true,
						isFixed: false,
					},
					{
						name: "解除预警时间",
						dataField: "signDateShow",
						width: 140,
						isShow: true,
						isFixed: false,
					},
					{
						name: "单据状态",
						dataField: "dbdStatusShow",
						width: 120,
						isShow: true,
						isFixed: false,
					},
					{
						name: "督办情况",
						dataField: "dbdRemark",
						width: 120,
						isShow: true,
						isFixed: false,
					},
					{
						type: 'operate',
						name: "操作",
						width: 60,
						isShow: true,
						isFixed: true,
					},
				]
			};
			$scope.setFixedWidth = function () {
				$scope.tbStyle = {
					scrollLeft: { left: $scope.tableConfig.fixedWidth + "px" }
				};
			};

			$scope.exportParam = {
				sectionParam: { ids: [] },
				allUrl: ybHttp.RequestUrl("device", "/invoicewarn/dbdExportAll")
			};

			$scope.reset = function () {
				$scope.condition = {};
			};

			function getDataList(curPage) {
				$scope.markInfo.allChecked = false;
				$scope.markInfo.curRow = -1;
				$scope.exportParam.sectionParam.ids = [];

				var service = "device";
				var reqUrl = "/invoicewarn/dbdlist";

				if (!!curPage) {
					$scope.pageInfo.curPage = curPage;
				}

				var param = {
					pageSize: $scope.pageInfo.pageSize,
					pageNum: $scope.pageInfo.curPage,
					filters: angular.copy($scope.condition)
				};
				ybHttp.Post(service, reqUrl, param).then(function (resp) {
					if (resp.code !== 0) {
						ybWidget.ShowMessage(resp, MESG_TYPE.Error);
						return;
					}
					angular.forEach(resp.value.list, function (item) {
						item.isChecked = false;
						item.postDateShow = $filter("YbDate")(item.postDate, 'yyyy-MM-dd HH:mm:ss');
						item.isSignShow = $filter("ProgressStatus")(item.isSign, "signState");
						item.warnDateShow = $filter("YbDate")(item.warnDate, 'yyyy-MM-dd HH:mm:ss');
						item.signDateShow = $filter("YbDate")(item.signDate, 'yyyy-MM-dd HH:mm:ss');
						item.dbdStatusShow = $filter("ProgressStatus")(item.dbdStatus, 'priceChangeDB');
					});
					$scope.pageInfo.totalRows = resp.value.total;
					$scope.pageInfo.totalPages = resp.value.pages;
					$scope.dataList = resp.value.list;
				});
			}


			$scope.switchToolbar = function (toolbar) {
				if (!!$scope.toolbarConfig["show" + toolbar]) {
					$scope.toolbarConfig["show" + toolbar] = false;
				} else {
					$timeout(function () {
						$scope.toolbarConfig["show" + toolbar] = true;
					});
				}
			};

			$scope.selectRow = function (index) {
				$scope.markInfo.curRow = index
			};

			$scope.search = function (curpage) {
				getDataList(curpage);
			};

			function getBillIds() {
				$scope.exportParam.sectionParam.ids = [];
				angular.forEach($scope.dataList, function (item) {
					if (!!item.isChecked) {
						$scope.exportParam.sectionParam.ids.push(item.id);
					}
				});
			}

			$scope.selectAll = function (list, type) {
				for (var i = 0, len = list.length; i < len; i++) {
					list[i].isChecked = angular.isUndefined(type) ? $scope.markInfo.allChecked : $scope.markInfo["allChecked" + type];
				}
				getBillIds();
			};

			function checkAll(list) {
				var selectAll = false;
				for (var i = 0, len = list.length; i < len; i++) {
					if (!list[i].isChecked) {
						selectAll = false;
						break;
					}
					selectAll = true;
				}
				return selectAll;
			}

			$scope.selectSingle = function (item, list, type) {
				var selectAll = false;
				if (!item.isChecked) {
					selectAll = false;
				} else {
					if (!checkAll(list)) {
						selectAll = false;
					} else {
						selectAll = true;
					}
				}
				if (angular.isDefined(type)) {
					$scope.markInfo["allChecked" + type] = selectAll;
				} else {
					$scope.markInfo.allChecked = selectAll;
				}
				getBillIds();
			};

			function handleUpdateDbRemark(ids) {
				ybWidget.ShowConfirm("确定督办？", '督办情况').then(function (val) {
					var service = "device";
					var reqUrl = "/invoicewarn/dbdUpdate";

					var param = {
						ids: ids,
						dbdRemark: val,
					};
					ybHttp.Post(service, reqUrl, param).then(function (resp) {
						if (resp.code !== 0) {
							ybWidget.ShowMessage(resp, MESG_TYPE.Error);
							return;
						}
						ybWidget.ShowToaster('success', '操作成功');
						getDataList();
					})
				});
			}

			$scope.updateDbRemarkBatch = function () {
				const selectedDatas = $scope.dataList.filter(df => df.isChecked);
				if (selectedDatas.length === 0) {
					ybWidget.ShowMessage('请选择需要操作的行');
					return;
				}
				const notYetDbDatas = selectedDatas.filter(sd => sd.dbdStatus==0) || []
				if (notYetDbDatas.length === 0) {
					ybWidget.ShowMessage('所选数据全部已督办');
					return;
				}

				handleUpdateDbRemark(notYetDbDatas.map(s => s.id));
			};

			$scope.createdThread = function() {
				const selectedDatas = $scope.dataList.filter(df => df.isChecked);
                if (selectedDatas.length === 0) {
                    ybWidget.ShowMessage('请选择需要操作的行');
                    return;
                }

                ybWidget.ShowMessage("确定要生成线索吗？", MESG_TYPE.Ask).then(function () {
                    var service = "device";
                    var reqUrl = "/clew/add";
					let params = {
						source:"销售管理",
						type:"发票未签收督办单",
						billNoList:selectedDatas.map(s => s.id)
					}
                    ybHttp.Post(service, reqUrl, params).then(function (resp) {
                        if (resp.code !== 0) {
                            ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                            return;
                        }
                        ybWidget.ShowToaster('success', '操作成功');
                        getDataList();
                    });
                })
			}
			$scope.updateDbRemark = function (item) {
				handleUpdateDbRemark([item.id]);
			};


			var btnPermissionCode = {
				invoiceNotSignedDbdEdit: window.ActionCode.invoiceNotSignedDbdEdit,
				invoiceNotSignedDbdExport: window.ActionCode.invoiceNotSignedDbdExport,
			};
			$scope.btnPermission = {
				invoiceNotSignedDbdEdit: true,
				invoiceNotSignedDbdExport: true,
			};
			function setBtnPermission() {
				for (var key in btnPermissionCode) {
					$scope.btnPermission[key] = UserContextService.CheckPermission([btnPermissionCode[key]]);
				}
			}


			(function () {
				$scope.setFixedWidth();
				if ($stateParams.billNo) {
					$scope.condition.id = $stateParams.billNo
					getDataList();
				}else {
					getDataList();
				}
				UserContextService.SetBtnPermission(setBtnPermission);
			})();
		}])
		;
})(window, window.angular);
